package com.sxbbc.plat.service;

import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.stereotype.Service;

import com.sxbbc.common.basics.utils.date.DateUtil;
import com.sxbbc.common.core.entity.jz.AgDonateDetail;
import com.sxbbc.common.core.entity.jz.CDonateDetail;
import com.sxbbc.common.core.entity.jz.CMoneyDetail;
import com.sxbbc.common.core.entity.jz.CPointDetail;
import com.sxbbc.common.core.entity.jz.CShareDetail;
import com.sxbbc.common.core.entity.jz.OrderDetail;
import com.sxbbc.common.core.entity.jz.OrderExpDetail;
import com.sxbbc.common.core.entity.jz.OrderMain;

/**
 * 导出功能 业务层
 *
 * @author leehao
 * @date 2019-05-07 17:34
 */
@Service
public class ExportService {

	public HSSFWorkbook exportCustomerMoney(List<CMoneyDetail> list) {
		// 行数
		int row_num = 0;
		// 第一步，创建一个webbook，对应一个Excel文件
		HSSFWorkbook wb = new HSSFWorkbook();
		// 第二步，在webbook中添加一个sheet,对应Excel文件中的sheet
		HSSFSheet sheet = wb.createSheet("会员金额明细表");
		// 第四步，创建单元格，并设置值表头 设置表头居中
		HSSFCellStyle style = wb.createCellStyle();
		// 创建一个居中格式
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER);

		// 标题
		HSSFRow row_header = sheet.createRow(row_num++);
		String[] headers = { "会员账号", "流水号", "类型", "收支", "原始金额", "操作金额", "最后金额", "创建时间", "备注" };
		for (int i = 0; i < headers.length; i++) {
			HSSFCell cell = row_header.createCell((short) i);
			// 设置cell编码解决中文高位字节截断
			cell.setCellValue(headers[i]);
			cell.setCellStyle(style);
		}
		// 3.单元格应用样式
		HSSFRow row_data;
		DateUtil date = DateUtil.getInstance();
		for (CMoneyDetail c : list) {
			row_data = sheet.createRow(row_num++);
			//
			HSSFCell cell = row_data.createCell((short) 0);
			cell.setCellValue(c.getPhone());
			//
			cell = row_data.createCell((short) 1);
			cell.setCellValue(c.getPayCode());
			//
			cell = row_data.createCell((short) 2);
			cell.setCellValue(c.getTypeName());
			//
			cell = row_data.createCell((short) 3);
			cell.setCellValue(c.getIsout() == 1 ? "收入" : "支出");
			//
			cell = row_data.createCell((short) 4);
			cell.setCellValue(c.getOriginal().toString());
			//
			cell = row_data.createCell((short) 5);
			cell.setCellValue(c.getCost().toString());
			//
			cell = row_data.createCell((short) 6);
			cell.setCellValue(c.getLast().toString());
			//
			cell = row_data.createCell((short) 7);
			cell.setCellValue(date.toDateString(c.getCreateTime(), date.DEFAULT_DATETIME_PATTERN));
			//
			cell = row_data.createCell((short) 8);
			cell.setCellValue(c.getRemark());
		}
		return wb;
	}

	public HSSFWorkbook exportCustomerPoint(List<CPointDetail> list) {
		// 行数
		int row_num = 0;
		// 第一步，创建一个webbook，对应一个Excel文件
		HSSFWorkbook wb = new HSSFWorkbook();
		// 第二步，在webbook中添加一个sheet,对应Excel文件中的sheet
		HSSFSheet sheet = wb.createSheet("会员积分明细表");
		// 第四步，创建单元格，并设置值表头 设置表头居中
		HSSFCellStyle style = wb.createCellStyle();
		// 创建一个居中格式
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER);

		// 标题
		HSSFRow row_header = sheet.createRow(row_num++);
		String[] headers = { "会员账号", "流水号", "类型", "收支", "原始积分", "操作积分", "最后积分", "创建时间", "备注" };
		for (int i = 0; i < headers.length; i++) {
			HSSFCell cell = row_header.createCell((short) i);
			// 设置cell编码解决中文高位字节截断
			cell.setCellValue(headers[i]);
			cell.setCellStyle(style);
		}
		// 3.单元格应用样式
		HSSFRow row_data;
		DateUtil date = DateUtil.getInstance();
		for (CPointDetail c : list) {
			row_data = sheet.createRow(row_num++);
			//
			HSSFCell cell = row_data.createCell((short) 0);
			cell.setCellValue(c.getPhone());
			//
			cell = row_data.createCell((short) 1);
			cell.setCellValue(c.getPayCode());
			//
			cell = row_data.createCell((short) 2);
			cell.setCellValue(c.getTypeName());
			//
			cell = row_data.createCell((short) 3);
			cell.setCellValue(c.getIsout() == 1 ? "收入" : "支出");
			//
			cell = row_data.createCell((short) 4);
			cell.setCellValue(c.getOriginal().toString());
			//
			cell = row_data.createCell((short) 5);
			cell.setCellValue(c.getCost().toString());
			//
			cell = row_data.createCell((short) 6);
			cell.setCellValue(c.getLast().toString());
			//
			cell = row_data.createCell((short) 7);
			cell.setCellValue(date.toDateString(c.getCreateTime(), date.DEFAULT_DATETIME_PATTERN));
			//
			cell = row_data.createCell((short) 8);
			cell.setCellValue(c.getRemark());
		}
		return wb;
	}

	/**
	 * 导出订单列表
	 * 
	 * @param records 订单数据
	 * @return HSSFWorkbook
	 */
	public HSSFWorkbook exportOrder(List<OrderMain> list) {
		// 行数
		int row_num = 0;
		// 第一步，创建一个webbook，对应一个Excel文件
		HSSFWorkbook wb = new HSSFWorkbook();
		// 第二步，在webbook中添加一个sheet,对应Excel文件中的sheet
		HSSFSheet sheet = wb.createSheet("会员积分明细表");
		// 第四步，创建单元格，并设置值表头 设置表头居中
		HSSFCellStyle style = wb.createCellStyle();
		// 创建一个居中格式
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

		HSSFCellStyle cellStyle = cellStyle(wb);
		// 标题
		HSSFRow row_header = sheet.createRow(row_num++);
		String[] headers = { "订单号", "所属店铺","创建时间", "订单类型", "订单状态", "支付方式", "订单总额", "实际支付", "创建时间", "商品编号", "商品名称", "商品规格",
				"商品数量","商品单价","收货人", "收货人电话", "收货地址" };
		for (int i = 0; i < headers.length; i++) {
			HSSFCell cell = row_header.createCell((short) i);
			// 设置cell编码解决中文高位字节截断
			cell.setCellValue(headers[i]);
			cell.setCellStyle(style);
		}
		// 3.单元格应用样式
		HSSFRow row_data;
		DateUtil date = DateUtil.getInstance();
		for (int i = 0; i < list.size(); i++) {
			OrderMain o = list.get(i);
			for (OrderDetail detail : o.getDetails()) {
				row_data = sheet.createRow(row_num++);
				// 订单号
				HSSFCell cell = row_data.createCell(0);
				cell.setCellValue(o.getOrderCode());
				cell.setCellStyle(cellStyle);
				// 交易单号
				cell = row_data.createCell(1);
				cell.setCellValue(o.getPayCode());
				cell.setCellStyle(cellStyle);
				// 所属店铺
				cell = row_data.createCell(2);
				cell.setCellValue(o.getBNickName());
				cell.setCellStyle(cellStyle);
				// 订单类型
				cell = row_data.createCell(3);
				cell.setCellValue(o.getOrderTypeName());
				cell.setCellStyle(cellStyle);
				// 订单状态
				cell = row_data.createCell(4);
				cell.setCellValue(o.getOrderStatusName());
				cell.setCellStyle(cellStyle);
				// 支付方式
				cell = row_data.createCell(5);
				cell.setCellValue(o.getPayTypeName());
				cell.setCellStyle(cellStyle);
				// 订单总额
				cell = row_data.createCell(6);
				cell.setCellValue(o.getGoodsMoney().doubleValue());
				cell.setCellStyle(cellStyle);
				// 实际支付
				cell = row_data.createCell(7);
				cell.setCellValue(o.getRealMoney().doubleValue());
				cell.setCellStyle(cellStyle);
				// 创建时间
				cell = row_data.createCell(8);
				cell.setCellValue(date.toDateString(o.getCreateTime(), date.DEFAULT_DATETIME_PATTERN));
				cell.setCellStyle(cellStyle);
				// 商品信息
				cell = row_data.createCell(9);
				cell.setCellValue(detail.getProCode());
				cell.setCellStyle(cellStyle);
				cell = row_data.createCell(10);
				cell.setCellValue(detail.getProName());
				cell.setCellStyle(cellStyle);
				cell = row_data.createCell(11);
				cell.setCellValue(detail.getProSpecName());
				cell.setCellStyle(cellStyle);
				cell = row_data.createCell(12);
				cell.setCellValue(detail.getQty());
				cell.setCellStyle(cellStyle);
				cell = row_data.createCell(13);
				cell.setCellValue(String.valueOf(detail.getPrice()));
				cell.setCellStyle(cellStyle);

				// 物流信息
				OrderExpDetail logisticsDetail = o.getExpDetail();
				if (logisticsDetail != null) {
					cell = row_data.createCell(14);
					cell.setCellValue(logisticsDetail.getReceiver());
					cell.setCellStyle(cellStyle);
					cell = row_data.createCell(15);
					cell.setCellValue(logisticsDetail.getPhone());
					cell.setCellStyle(cellStyle);
					cell = row_data.createCell(16);
					cell.setCellValue(logisticsDetail.getAddrPrefix() + " " + logisticsDetail.getAddressDetail());
					cell.setCellStyle(cellStyle);
				}
			}
			for (int k = 0; k <= 8; k++) {
				sheet.addMergedRegion(new CellRangeAddress(row_num - o.getDetails().size(), row_num - 1, k, k));
			}
			for (int k = 14; k <= 16; k++) {
				sheet.addMergedRegion(new CellRangeAddress(row_num - o.getDetails().size(), row_num - 1, k, k));
			}
			// 必须在单元格设值以后进行
            // 设置为根据内容自动调整列宽    
            sheet.autoSizeColumn(i);
		}
		return wb;
	}

	public static HSSFCellStyle cellStyle(HSSFWorkbook wb) {
		HSSFCellStyle cellStyle = wb.createCellStyle();// 新建单元格样式
        //cellStyle.setWrapText(true);//自动换行
		// 设置背景颜色
		//cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
		// 边框
		// 下边框  
		cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		// 左边框  
		cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		// 上边框
		cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
		// 右边框 
		cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
		// 居中
		// 水平居中
		cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		// 垂直居中
		cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

		return cellStyle;
	}

	public HSSFWorkbook exportCustomerDonate(List<CDonateDetail> list) {
		// 行数
		int row_num = 0;
		// 第一步，创建一个webbook，对应一个Excel文件
		HSSFWorkbook wb = new HSSFWorkbook();
		// 第二步，在webbook中添加一个sheet,对应Excel文件中的sheet
		HSSFSheet sheet = wb.createSheet("会员贡献值明细表");
		// 第四步，创建单元格，并设置值表头 设置表头居中
		HSSFCellStyle style = wb.createCellStyle();
		// 创建一个居中格式
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER);

		// 标题
		HSSFRow row_header = sheet.createRow(row_num++);
		String[] headers = { "会员账号", "流水号", "类型", "收支", "原始贡献值", "操作贡献值", "最后贡献值", "创建时间", "备注" };
		for (int i = 0; i < headers.length; i++) {
			HSSFCell cell = row_header.createCell((short) i);
			// 设置cell编码解决中文高位字节截断
			cell.setCellValue(headers[i]);
			cell.setCellStyle(style);
		}
		// 3.单元格应用样式
		HSSFRow row_data;
		DateUtil date = DateUtil.getInstance();
		for (CDonateDetail c : list) {
			row_data = sheet.createRow(row_num++);
			//
			HSSFCell cell = row_data.createCell((short) 0);
			cell.setCellValue(c.getPhone());
			//
			cell = row_data.createCell((short) 1);
			cell.setCellValue(c.getPayCode());
			//
			cell = row_data.createCell((short) 2);
			cell.setCellValue(c.getTypeName());
			//
			cell = row_data.createCell((short) 3);
			cell.setCellValue(c.getIsout() == 1 ? "收入" : "支出");
			//
			cell = row_data.createCell((short) 4);
			cell.setCellValue(c.getOriginal().toString());
			//
			cell = row_data.createCell((short) 5);
			cell.setCellValue(c.getCost().toString());
			//
			cell = row_data.createCell((short) 6);
			cell.setCellValue(c.getLast().toString());
			//
			cell = row_data.createCell((short) 7);
			cell.setCellValue(date.toDateString(c.getCreateTime(), date.DEFAULT_DATETIME_PATTERN));
			//
			cell = row_data.createCell((short) 8);
			cell.setCellValue(c.getRemark());
		}
		return wb;
	}

	public HSSFWorkbook exportCustomerShare(List<CShareDetail> list) {
		// 行数
		int row_num = 0;
		// 第一步，创建一个webbook，对应一个Excel文件
		HSSFWorkbook wb = new HSSFWorkbook();
		// 第二步，在webbook中添加一个sheet,对应Excel文件中的sheet
		HSSFSheet sheet = wb.createSheet("会员共享明细表");
		// 第四步，创建单元格，并设置值表头 设置表头居中
		HSSFCellStyle style = wb.createCellStyle();
		// 创建一个居中格式
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER);

		// 标题
		HSSFRow row_header = sheet.createRow(row_num++);
		String[] headers = { "会员账号", "流水号", "类型", "收支", "原始共享值", "操作共享值", "最后共享值", "创建时间", "备注" };
		for (int i = 0; i < headers.length; i++) {
			HSSFCell cell = row_header.createCell((short) i);
			// 设置cell编码解决中文高位字节截断
			cell.setCellValue(headers[i]);
			cell.setCellStyle(style);
		}
		// 3.单元格应用样式
		HSSFRow row_data;
		DateUtil date = DateUtil.getInstance();
		for (CShareDetail c : list) {
			row_data = sheet.createRow(row_num++);
			//
			HSSFCell cell = row_data.createCell((short) 0);
			cell.setCellValue(c.getPhone());
			//
			cell = row_data.createCell((short) 1);
			cell.setCellValue(c.getPayCode());
			//
			cell = row_data.createCell((short) 2);
			cell.setCellValue(c.getTypeName());
			//
			cell = row_data.createCell((short) 3);
			cell.setCellValue(c.getIsout() == 1 ? "收入" : "支出");
			//
			cell = row_data.createCell((short) 4);
			cell.setCellValue(c.getOriginal().toString());
			//
			cell = row_data.createCell((short) 5);
			cell.setCellValue(c.getCost().toString());
			//
			cell = row_data.createCell((short) 6);
			cell.setCellValue(c.getLast().toString());
			//
			cell = row_data.createCell((short) 7);
			cell.setCellValue(date.toDateString(c.getCreateTime(), date.DEFAULT_DATETIME_PATTERN));
			//
			cell = row_data.createCell((short) 8);
			cell.setCellValue(c.getRemark());
		}
		return wb;
	}

	public HSSFWorkbook exportAgentDonate(List<AgDonateDetail> list) {
		// 行数
		int row_num = 0;
		// 第一步，创建一个webbook，对应一个Excel文件
		HSSFWorkbook wb = new HSSFWorkbook();
		// 第二步，在webbook中添加一个sheet,对应Excel文件中的sheet
		HSSFSheet sheet = wb.createSheet("代理贡献明细表");
		// 第四步，创建单元格，并设置值表头 设置表头居中
		HSSFCellStyle style = wb.createCellStyle();
		// 创建一个居中格式
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER);

		// 标题
		HSSFRow row_header = sheet.createRow(row_num++);
		String[] headers = { "流水号", "类型", "收支", "原始贡献值", "操作贡献值", "最后贡献值", "创建时间", "备注" };
		for (int i = 0; i < headers.length; i++) {
			HSSFCell cell = row_header.createCell((short) i);
			// 设置cell编码解决中文高位字节截断
			cell.setCellValue(headers[i]);
			cell.setCellStyle(style);
		}
		// 3.单元格应用样式
		HSSFRow row_data;
		DateUtil date = DateUtil.getInstance();
		for (AgDonateDetail c : list) {
			row_data = sheet.createRow(row_num++);
			//
			HSSFCell cell = row_data.createCell((short) 0);
			cell.setCellValue(c.getPayCode());
			//
			cell = row_data.createCell((short) 1);
			cell.setCellValue(c.getTypeName());
			//
			cell = row_data.createCell((short) 2);
			cell.setCellValue(c.getIsout() == 1 ? "收入" : "支出");
			//
			cell = row_data.createCell((short) 3);
			cell.setCellValue(c.getOriginal().toString());
			//
			cell = row_data.createCell((short) 4);
			cell.setCellValue(c.getCost().toString());
			//
			cell = row_data.createCell((short) 5);
			cell.setCellValue(c.getLast().toString());
			//
			cell = row_data.createCell((short) 6);
			cell.setCellValue(date.toDateString(c.getCreateTime(), date.DEFAULT_DATETIME_PATTERN));
			//
			cell = row_data.createCell((short) 7);
			cell.setCellValue(c.getRemark());
		}
		return wb;
	}
}
